package com.hongxun.budget.ui.fragment.sg;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.hongxun.budget.R;
import com.hongxun.budget.ui.activity.sg.MaterialArriveRecordAddActivity;
import com.hongxun.budget.ui.dialog.ListDialog;
import com.hongxun.budget.ui.dialog.NoticeDialog;
import com.hongxun.budget.ui.fragment.BaseListFragment;
import com.hongxun.budget.utils.DateUtils;
import com.hongxun.budget.utils.RecyclerViewUtils;
import com.hongxun.budget.utils.SPUtils;
import com.hongxun.budget.utils.SgDBUtils;
import com.hongxun.budget.utils.SgStrKey;
import com.hongxun.budget.utils.XUtils;
import com.hongxun.greenentity.sg.CArrivalRecordEntity;
import com.hongxun.greenentity.sg.CStackPointEntity;
import com.hongxun.interfaces.ClickListener;
import com.hongxun.interfaces.ResponseListener;
import com.jessewu.library.SuperAdapter;
import com.jessewu.library.view.ViewHolder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * 到货记录列表展示
 */
public class MaterialPresentRecordList extends BaseListFragment {
    @BindView(R.id.tv1)
    TextView tv1;

    private SuperAdapter<CArrivalRecordEntity> superAdapter;
    private List<CArrivalRecordEntity> entityList = new ArrayList<>();
    private List<CArrivalRecordEntity> entityListAll = new ArrayList<>();
    private List<CStackPointEntity> list = new ArrayList<>();
    private Map<String, String> map = new HashMap<>();

    @Override
    protected int getMyListLayoutId() {
        return R.layout.sg_fragment_materialpresentrecordlist;
    }

    @Override
    public RecyclerView.LayoutManager getLayoutManager() {
        return RecyclerViewUtils.getLinearLayoutManagerForVertical(context);
    }

    @Override
    public SuperAdapter getAdapter() {
        superAdapter = new SuperAdapter<CArrivalRecordEntity>(R.layout.sg_content_mpd_adapter) {
            @Override
            public void bindView(ViewHolder viewHolder, CArrivalRecordEntity entity, int i) {
                try {
                    ImageView imageView = viewHolder.getView(R.id.iv1);
                    Object ssUrl = SgDBUtils.getSsUrl(entity.getGuid());
                    XUtils.loadImg(context, ssUrl, imageView);
                    ((TextView) viewHolder.getView(R.id.tv6_metarial)).setText(map.get(entity.getGuid()));
                    ((TextView) viewHolder.getView(R.id.tv7_time)).setText(DateUtils.LongToStringyyMMdd_(entity.getARTime().getTime()));
                    ((TextView) viewHolder.getView(R.id.tv8_count)).setText(entity.getSPName());
                } catch (NullPointerException e) {
                }
            }
        };
        superAdapter.setOnItemClickListener(new SuperAdapter.OnItemClickListener<CArrivalRecordEntity>() {
            @Override
            public void onClick(int i, CArrivalRecordEntity entity) {
                SPUtils.put(context, SgStrKey.uuiddh, entity.getGuid());
                startActivity(new Intent(context, MaterialArriveRecordAddActivity.class)
                        .putExtra("isNewAdd", false));
            }
        });
        superAdapter.setOnItemLongClickListener(new SuperAdapter.OnItemLongClickListener<CArrivalRecordEntity>() {
            @Override
            public void onLongClick(int i, CArrivalRecordEntity entity) {
                longClickDel(entity);
            }
        });
        superAdapter.setData(entityList);
        return superAdapter;
    }

    //删除项目
    private void longClickDel(final CArrivalRecordEntity dhjl) {
        new NoticeDialog()
                .setTitle("删除记录")
                .setMsg(map.get(dhjl.getGuid()))
                .setCancelButton("取消", null)
                .setConfirmButton("删除", new ClickListener() {
                    @Override
                    public void click() {
                        entityList.remove(dhjl);
                        superAdapter.notifyDataSetChanged();
                        SgDBUtils.delCArrivalRecordEntity(dhjl.getGuid());
                    }
                }).show(getFragmentManager(), "");
    }

    @OnClick({R.id.tv1, R.id.tv4_add})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv1:
                chioceDfd();
                break;
            case R.id.tv4_add:
                if (list.size() - 1 == 0) {
                    showMsg("请在地图上增加物资堆放点");
                    return;
                }
                startActivity(new Intent(context, MaterialArriveRecordAddActivity.class)
                        .putExtra("isNewAdd", true));
                break;
        }
    }

    private void chioceDfd() {
        new ListDialog<CStackPointEntity>()
                .setTitle("选择物资堆放点")
                .setDatas(list)
                .setListener(new ResponseListener() {
                    @Override
                    public void response(Object... data) {
                        checkPointData(data[0]);
                    }
                }).show(getFragmentManager(), "");
    }

    private void checkPointData(Object obj) {
        CStackPointEntity entity = (CStackPointEntity) obj;
        tv1.setText(entity.getSPName());
        Observable.just(entity.getGuid()).subscribeOn(Schedulers.io()).map(new Func1<String, Boolean>() {
            @Override
            public Boolean call(String guid) {
                entityList.clear();
                for (CArrivalRecordEntity recordEntity : entityListAll) {
                    if (guid.isEmpty() || recordEntity.getSPGuid().equals(guid)) {
                        entityList.add(recordEntity);
                    }
                }
                return true;
            }
        }).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Boolean>() {
            @Override
            public void call(Boolean b) {
                superAdapter.notifyDataSetChanged();
            }
        });

    }

    @Override
    public void onRefresh() {
        map.clear();
        list.clear();
        entityList.clear();
        entityListAll.clear();
        Observable.just(SgStrKey.uuidxm).subscribeOn(Schedulers.io()).map(new Func1<String, Boolean>() {
            @Override
            public Boolean call(String value) {
                CStackPointEntity pointEntity = new CStackPointEntity();
                pointEntity.setGuid("");
                pointEntity.setSPName("全部");
                list.add(pointEntity);
                //获取堆放点
                String xmid = String.valueOf(SPUtils.get(context, value, ""));
                list.addAll(SgDBUtils.getCSpListAll(xmid));
                //获取到货记录
                entityList.addAll(SgDBUtils.getCArListByPGuid(xmid));
                entityListAll.addAll(entityList);
                for (CArrivalRecordEntity entity : entityList) {
                    map.put(entity.getGuid(), SgDBUtils.getCGdListTj(entity.getGuid()));
                }
                return true;
            }
        }).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Boolean>() {
            @Override
            public void call(Boolean b) {
                superAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        onRefresh();
    }

}
